package com.example.aspect.common;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Copyright 上海危网信息科技有限公司版权所有
 * @company wwsoft
 * @Date 2019/11/20 16:30
 */
@Aspect
@Component
@Slf4j
public class ErrorAspect {
    private static ThreadLocal<ProceedingJoinPoint> threadLocal = new ThreadLocal<>();

    @Pointcut("execution(* com.example.*..*.*(..))")
    @Order(1)
    public void pointCut( ){}


    @AfterThrowing(throwing = "throwable",pointcut = "pointCut()")
    public void recordException(JoinPoint point,Throwable throwable) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        log.error("\n请求URL:{}",request.getRequestURL());
        log.error("异常信息: {}.{}.({}) : message:{}",method.getDeclaringClass().getName(),method.getName(),point.getArgs(),throwable.getMessage(),throwable);
    }
}
